package com.wanmait.movie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.wanmait.movie.entity.Cinema;
import com.wanmait.movie.entity.Lobby;
import com.wanmait.movie.entity.Movie;
import com.wanmait.movie.entity.Screening;
import com.wanmait.movie.entity.VO.ScreeningDTO;
import com.wanmait.movie.mapper.LobbyMapper;
import com.wanmait.movie.mapper.ScreeningMapper;
import com.wanmait.movie.service.ScreeningService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ScreeningServiceImpl extends ServiceImpl<ScreeningMapper, Screening> implements ScreeningService {
    @Resource
    private ScreeningMapper screeningMapper;
    @Resource
    private LobbyMapper lobbyMapper;

    @Override
    public PageInfo<ScreeningDTO> getScreeningList(Integer pageNum, Integer cinemaId, Integer lobbyId, String keyword) {
        PageHelper.startPage(pageNum, 10);
        MPJLambdaWrapper<Lobby> lobbyMPJLambdaWrapper = new MPJLambdaWrapper<>();
        lobbyMPJLambdaWrapper
                .select(Lobby::getLobbyName)
                .select(Cinema::getCinemaName)
                .select(Movie::getMovieName)
                .select(Screening::getId,Screening::getMovieId,
                        Screening::getEndTime,Screening::getLobbyId,
                        Screening::getMoney, Screening::getStartTime)
                .leftJoin(Screening.class, Screening::getLobbyId, Lobby::getId)
                .leftJoin(Cinema.class, Cinema::getId, Lobby::getCinemaId)
                .leftJoin(Movie.class, Movie::getId, Screening::getMovieId)
                .gt(Screening::getEndTime, new Date());
        if (lobbyId != 0) {
            lobbyMPJLambdaWrapper.eq(Lobby::getId, lobbyId);
            return new PageInfo<>(lobbyMapper.selectJoinList(ScreeningDTO.class, lobbyMPJLambdaWrapper));
        } else {
            if (cinemaId != 0) {
                lobbyMPJLambdaWrapper.in(Lobby::getCinemaId, cinemaId);
                return new PageInfo<>(lobbyMapper.selectJoinList(ScreeningDTO.class, lobbyMPJLambdaWrapper));
            } else {
                if (keyword != "") {
                    lobbyMPJLambdaWrapper.like(Cinema::getCinemaName, keyword);
                    return new PageInfo<>(lobbyMapper.selectJoinList(ScreeningDTO.class, lobbyMPJLambdaWrapper));
                }
            }
        }
        return new PageInfo<>(lobbyMapper.selectJoinList(ScreeningDTO.class, lobbyMPJLambdaWrapper));
    }

    @Override
    public Screening getScreeningInfo(Integer id) {
        return screeningMapper.selectById(id);
    }

    @Override
    public List<ScreeningDTO> shopList(Integer movieId, Integer cinemaId) {
        MPJLambdaWrapper<Screening> wrapper = new MPJLambdaWrapper<>();
        wrapper.select(Screening::getId,Screening::getMovieId
                ,Screening::getLobbyId,Screening::getMoney
                ,Screening::getStartTime,Screening::getEndTime)
                .select(Lobby::getLobbyName)
                .select(Cinema::getCinemaName,Cinema::getAddress)
                .select(Movie::getMovieName,Movie::getLanguage,Movie::getScore
                ,Movie::getPeoples,Movie::getMoviePic,Movie::getTime)
                .leftJoin(Lobby.class,Lobby::getId,Screening::getLobbyId)
                .leftJoin(Cinema.class,Cinema::getId,Lobby::getCinemaId)
                .leftJoin(Movie.class,Movie::getId,Screening::getMovieId)
                .eq(Movie::getId, movieId)
                .eq(Cinema::getId, cinemaId);
        return  (screeningMapper.selectJoinList(ScreeningDTO.class,wrapper));
    }
    public List<ScreeningDTO> shopping(Integer movieId) {
        MPJLambdaWrapper<Screening> wrapper = new MPJLambdaWrapper<>();
        wrapper.select(Screening::getId, Screening::getMovieId
                , Screening::getLobbyId, Screening::getMoney
                , Screening::getStartTime, Screening::getEndTime)
                .select(Lobby::getLobbyName)
                .select(Cinema::getCinemaName, Cinema::getAddress)
                .select(Movie::getMovieName, Movie::getLanguage, Movie::getScore
                        , Movie::getPeoples, Movie::getMoviePic, Movie::getTime)
                .leftJoin(Lobby.class, Lobby::getId, Screening::getLobbyId)
                .leftJoin(Cinema.class, Cinema::getId, Lobby::getCinemaId)
                .leftJoin(Movie.class, Movie::getId, Screening::getMovieId)
                .eq(Screening::getId, movieId);
        return (screeningMapper.selectJoinList(ScreeningDTO.class, wrapper));
    }
}
